1.当前文件路径的导入1
2base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,base_path)
2.配置库的导入1
2
3
4
5
6
7
8
9
10# 数据库配置
MYSQL_INFO = {
'host': 'XXX',
'user': 'root',
'password': 'XXX',
'port': 3307,
'db': 'shuiping',
'charset': 'utf8',
'autocommit': True
}
1 | coon = pymysql.connect(**MYSQL_INFO) |
3.接口配置1
2
3
4
5
6
7
8import flask
server = flask.Flask(__name__) # 实例化server对象
@server.route('/login',methods=["get","post"]) # server.route装饰器装饰方法
def api_login():
return "my first api"
server.run(port=8999,debug=True) # server的启动
4.请求信息的读取1
2
3
4
5
6
7
8
9
10# 获取get的参数
username = flask.request.args.get("username")
pwd = flask.request.args.get("password")
# 获取post - kv的参数
username = flask.request.values.get("username")
pwd = flask.request.values.get("password")
# 获取post - json的参数
jsonData = flask.request.json
username = jsonData.get("username")
pwd = jsonData.get("password")
5.请求内容的判断1
2
3method = flask.request.method
if method == "GET":
elif method == "POST":